package com.meitianhui.convenitenbuy.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.meitianhui.convenitenbuy.R;

/**
 * Created by Administrator on 2017/11/13.
 */

public class CustomTipDialog extends Dialog implements View.OnClickListener {
    private Context context;
    private CustomTipDialog dialog = null;
    public DialogClick dialogClick;
    Button mBtnConfirm, mBtnCancel;
    EditText mEtContent;
    private TextView mTvTitle, mTvContent;

    public CustomTipDialog(Context context) {
        super(context);
    }
    public CustomTipDialog(Context context, int theme) {
        super(context, theme);
    }
    public CustomTipDialog(Context context, DialogClick dialogClick) {
        super(context);
        this.context = context;
        this.dialogClick = dialogClick;
        dialog = new CustomTipDialog(context, R.style.dialog_with_alpha);
        dialog.setContentView(R.layout.dialog_order_operation);
        dialog.setCanceledOnTouchOutside(true);
        initView(dialog);
        dialog.show();
        //设置dialog的宽度和高度
        try {
            Window win = dialog.getWindow();
            win.getDecorView().setPadding(80, 0, 80, 0);
            WindowManager.LayoutParams lp = win.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
            win.setAttributes(lp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    /**
     * 布局绑定
     *
     * @param dialog
     */
    private void initView(CustomTipDialog dialog) {
        mBtnCancel = (Button) dialog.findViewById(R.id.btn_cancel);
        mBtnCancel.setOnClickListener(this);
        mBtnConfirm = (Button) dialog.findViewById(R.id.btn_confirm);
        mBtnConfirm.setOnClickListener(this);
        mEtContent = (EditText) dialog.findViewById(R.id.et_content);
        mTvTitle = (TextView) dialog.findViewById(R.id.tv_title);
        mTvContent = (TextView) dialog.findViewById(R.id.tv_content);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_cancel:    //取消
                if (dialogClick != null) {
                    dialogClick.cancelClick();
                }
                break;
            case R.id.btn_confirm:   //确认
                if (dialogClick != null) {
                    dialogClick.submitClick();
                }
                break;
        }
    }


    /**
     * 获取输入内容
     *
     * @return
     */
    public String getContent() {
        String account = mEtContent.getText().toString();
        return account;
    }

    /**
     * 设置标题
     * @param title
     */
    public void setTitle(String title) {
        mTvTitle.setText(title);
    }

    /**
     * 设置内容
     * @param content
     */
    public void setContent(String content) {
        mTvContent.setText(content);
    }

    /**
     * 是否可见
     * @param show
     */
    public void setVisible(boolean show) {
        mEtContent.setVisibility(show ? View.VISIBLE : View.GONE);
    }


    public void stopProgressDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog = null;
        }
    }

    public void setOnClickListener(DialogClick l) {
        this.dialogClick = l;
    }

    public interface DialogClick {

        void cancelClick();

        void submitClick();

    }

}